﻿using MLSMooc.BO;
using MLSMooc.Service.Interface.Exam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MLSMooc.Helper;
using Dapper;
using MLSMooc.DAO.Implement.Exam;

namespace MLSMooc.Service.Implement.Exam
{
    public class ExamServiceImpl : IExamServiceImpl
    {
        private static ExamServiceImpl instance = new ExamServiceImpl();
        private ExamServiceImpl() { }
        public static ExamServiceImpl Getinstance() => instance;

        ExamDAO examDAO = ExamDAO.Getinstance();

        //提交答题
        public async Task<int> InsertExamAnswer(List<Exam_AnswerBO> data)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"insert into Mooc_Exam_Answer values(@Eid,@AdminId,@SelectOption,@Score,@AnswerNumber,getdate(),getdate(),0)");
                var successcount = 0;// 添加计数
                var AnswerNumber = await examDAO.GetExamMaxAnswerNumber(data[0].AdminId);//获取答题用户答题次数
                for (int i = 0; i < data.Count; i++)
                {
                    var examscour = await examDAO.GetExamScore(data[i].CourseId, data[i].Eid);//获取正确答案
                    var score=0;
                    if (examscour.RightAnswerText== data[i].SelectOption)
                    {
                        score = 1;
                    }
                    
                    var param = new
                    {
                        Eid = data[i].Eid,
                        AdminId = data[i].AdminId,
                        SelectOption = data[i].SelectOption,
                        Score = score,
                        AnswerNumber=AnswerNumber.AnswerNumber+1
                    };
                    successcount += await conn.ExecuteAsync(sql.ToString(), param);
                }
                if (successcount==data.Count)
                {
                    return 1;

                }
                else
                {
                    return 0;
                }
            }
        }

        public async Task<string> VideoExamAnswer(List<Exam_AnswerBO> data)
        {
            string strmsg = string.Empty;
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                for (int i = 0; i < data.Count; i++)
                {
                    var examscour = await examDAO.GetExamScore(data[i].CourseId, data[i].Eid);//获取正确答案
                   
                    if (examscour.RightAnswerText == data[i].SelectOption)
                    {
                        strmsg = "回答正确";
                      
                    }
                    else
                    {
                        strmsg = "正确答案是："+examscour.RightAnswerText;
                    }
                  
                }
                return strmsg.Trim();
            }
        }

        public async Task<string> VideoTanKExamAnswer(List<Exam_AnswerBO> data)
        {
            string strmsg = string.Empty;
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                for (int i = 0; i < data.Count; i++)
                {
                    var examscour = await examDAO.GetTankExamScore(data[i].CourseId, data[i].Eid);//获取新建表弹出框选择正确答案

                    if (examscour.RightAnswerText == data[i].SelectOption)
                    {
                        strmsg = "回答正确";

                    }
                    else
                    {
                        strmsg = "正确答案是：" + examscour.RightAnswerText;
                    }

                }
                return strmsg.Trim();
            }
        }

        //获取Mooc个人测试得分列表
        public async Task<IEnumerable<Mooc_ExamAnswerBO>> GetByAIdMoocExamList(int Aid, int CId)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {

                StringBuilder sql = new StringBuilder($@" select mc.Course_Name,AnswerNumber,sum(mea.Score) Score from Mooc_Exam me join Mooc_Exam_Answer mea on me.Id=mea.Eid join Mooc_Course mc on me.CourseId=mc.Id  where  ME.CourseId=@Cid and mea.AdminId=@Aid and me.isDelete=0 and mea.isDelete=0 group by mc.Course_Name,AnswerNumber ");
                var res = await conn.QueryAsync<Mooc_ExamAnswerBO>(sql.ToString(), new { Aid = Aid, Cid = CId });
                return res;
            }
        }

        //获取Mooc个人测试得分详情
        public async Task<IEnumerable<Mooc_ExamAnswerBO>> GetMoocScoreInfo(int Aid, int Cid)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"  select AnswerNumber, me.Topic,me.TopicType,me.RightAnswerText,mea.SelectOption,mea.Score 
from Mooc_Exam me 
join Mooc_Exam_Answer mea on me.Id=mea.Eid 
join Mooc_Course mc on me.CourseId=mc.Id 
where  me.CourseId=@Cid and mea.AdminId=@Aid and me.isDelete=0 and mea.isDelete=0 
and AnswerNumber=(select max(AnswerNumber)
from Mooc_Exam me 
join Mooc_Exam_Answer mea on me.Id=mea.Eid 
join Mooc_Course mc on me.CourseId=mc.Id  where  me.CourseId=@Cid and mea.AdminId=@Aid )   ");
                var res = await conn.QueryAsync<Mooc_ExamAnswerBO>(sql.ToString(), new { Aid = Aid, Cid = Cid });
                return res;
            }
        }

        //删除测试题
        public async Task<int> DeleteExaminfo(int id)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"update Mooc_ExamNew set isDelete=1 where Id=@Id");
                var res = await conn.ExecuteAsync(sql.ToString(), new { Id = id });
                return res;
            }
        }

        //修改测试题
        public async Task<int> UpdateExaminfo(ExamBO data)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"update Mooc_ExamNew set Topic=@Topic,Img=@Img,TopicType=@TopicType,OptionA=@OptionA,OptionB=@OptionB,OptionC=@OptionC,OptionD=@OptionD,OptionE=@OptionE,OptionTrue=@OptionTrue,OptionFalse=@OptionFalse,RightAnswer=@RightAnswer,RightAnswerText=@RightAnswerText,UpdateTime=@UpdateTime where Id=@Id");

                object param = new
                {
                    Id = data.Id,
                    Topic = data.Topic,
                    Img = "https://www.mlslab.cn/img/Mooc_Examimg/" + data.Img,
                    TopicType = data.TopicType,
                    OptionA = data.OptionA,
                    OptionB = data.OptionB,
                    OptionC = data.OptionC,
                    OptionD = data.OptionD,
                    OptionE = data.OptionE,
                    OptionTrue = data.OptionTrue,
                    OptionFalse = data.OptionFalse,
                    RightAnswer = data.RightAnswer,
                    //Score = data.Score,
                    UpdateTime = DateTime.Now,
                    RightAnswerText = data.RightAnswerText
                };
                var res = await conn.ExecuteAsync(sql.ToString(), param);
                return res;
            }
        }

        //添加测试题
        public async Task<int> InsertExamitem(ExamBO data)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"insert into Mooc_ExamNew values(@CourseId,@Topic,@Img,@TopicType,@OptionA,@OptionB,@OptionC,@OptionD,@OptionE,@OptionTrue,@OptionFalse,@RightAnswer,@RightAnswerText,0,Getdate(),getdate(),0)");
                object param = new
                {
                    CourseId = data.CourseId,
                    Topic = data.Topic,
                    Img = "https://www.mlslab.cn/img/Mooc_Examimg/" + data.Img,
                    TopicType = data.TopicType,
                    OptionA = data.OptionA,
                    OptionB = data.OptionB,
                    OptionC = data.OptionC,
                    OptionD = data.OptionD,
                    OptionE = data.OptionE,
                    OptionTrue = data.OptionTrue,
                    OptionFalse = data.OptionFalse,
                    RightAnswer = data.RightAnswer,
                    //Score = data.Score,
                    RightAnswerText = data.RightAnswerText
                };

                var res = await conn.ExecuteAsync(sql.ToString(), param);
                return res;
            }
        }

    }
}
